package com.sunnyebuy.merchant.service.impl;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sunnyebuy.merchant.dao.StateDAO;
import com.sunnyebuy.merchant.service.StateService;

/**
 * <p>
 * Title: StateServiceImpl.java
 * </p>
 * 
 * <p>
 * Description: com.sunnyebuy.merchant.service.impl
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008-2011 All rights reserved.
 * </p>
 * 
 * <p>
 * Created: 2011-12-28 上午11:22:17
 * </p>
 * 
 * @author Wenzhong Gu
 * 
 * @version 1.0
 */
@Service(value = "stateService")
public class StateServiceImpl implements StateService {

	@Resource
	private StateDAO stateDAO;

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	@Cacheable(value = "state", key = "#countryAbbreviation+'states'")
	public Map<String, String> findStatesByCountryAbbreviation(String countryAbbreviation) {
		try {
			List<?> states = stateDAO.findStatesByCountryAbbreviation(countryAbbreviation);
			Map<String, String> stateMap = new TreeMap<String, String>();
			String[] state;
			for (int i = 0; i < states.size(); i++) {
				state = (String[]) states.get(i);
				stateMap.put(state[0], state[1]);
			}
			return stateMap;
		} catch (Exception e) {
			return null;
		}
	}
}